<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入会员名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item prop="time" label="处理时间:">
        <el-date-picker
          v-model="queryParams.modifyTime"
          type="datetime"
          @change="chooseTime()"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button>
          <el-button icon="el-icon-download" 
          size="mini" 
          @click="exportTable"
          >导出</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="dealPayTime('add')"
          v-hasPermi="['member:dealPayMessage:index']"
          >新增</el-button>
      </el-col> 
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bookkeepList"
    >
      <el-table-column label="序号" prop="id" align="center"/>
      <el-table-column label="成员名称" align="center" prop="memberName"/>
      <el-table-column label="充值金额" align="center" prop="money"/>
      <el-table-column label="会员充值时间" align="center" prop="beginTime">
        <template  slot-scope="scope">
          {{ scope.row.beginTime }} - {{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="充值天数" align="center" prop="memberDay"/>
      <el-table-column label="缴费时间" align="center" prop="paymentTime"/>
      <el-table-column label="处理时间" align="center" prop="modifyTime"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="dealPayTime(scope.row.bid)"
            v-hasPermi="['member:dealPayMessage:index']"
            :disabled="new Date().getTime() - scope.row.isDeal > 2592000000 || scope.row.status == 0"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="delMessage(scope.row.bid)"
            :disabled="new Date().getTime() - scope.row.isDeal > 2592000000 || scope.row.status == 0"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { bookkeepList,delMemberMessage } from '@/api/member'
export default {
  data() {
    return {
      //弹窗
      open:false,
      //显示搜索条件
      showSearch:true,
      logrules: {},
      // 遮罩层
      loading: true,
      // 选中数组
      sid: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //账单数据
      bookkeepList:[],
      //剩余时间
      RemainingTime:'',
      //到期时间
      endTime:'',
      //时间限制
      pickerOptions:{},
      // 查询参数
      queryParams: {
        pageSize:10,
        pageNum:1,
        modifyTime:'',
        keyWord:'',
      },
      //提交参数
      form:{
        beginTime:'',
        endTime:'',
        modifyTime:'',
        money:'',
        mid:'',
        paymentTime:'',
      },
      //有效时间
      effectiveTime:'',
      //操作权限
      isDeal:'',
    };
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      let params = this.queryParams
      bookkeepList(params).then(res=>{
        if(res.code === 200){
          this.bookkeepList = res.rows
          this.total=res.total
          this.bookkeepList.forEach((item,index)=>{
            this.$set(item, 'id', index + 1)
            this.$set(item, 'isDeal', Date.parse(item.modifyTime))
            const time = [new Date(item.endTime).getTime()-new Date(item.beginTime).getTime()]/(1000*60*60*24)
            this.$set(item,'memberDay',parseInt(time))
            item.money = this.keepTwoDigits(item.money)
          })
        }
      })
      this.loading = false
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageSize:10,
        pageNum:1,
        modifyTime:'',
        keyWord:'',
      },
      this.handleQuery();
    },
    //导出操作
    exportTable(){
      this.download(
        "bookkeeping/export",
        {
          ...this.queryParams,
        },
        `Payment_${new Date().getTime()}.xlsx`
      );
    },
    //操作缴费时间
    dealPayTime(data){
      if(data == 'add'){
        this.$router.push({
          path: "/member/dealPayMessage",
          query:{key:'add'}
        });
      }else{
        this.$router.push({
          path: "/member/dealPayMessage",
          query:{key:data}
        });
      }
    },
    //重置信息
    reset(){
      this.effectiveTime='',
      this.RemainingTime='',
      this.form = {
        memberNo:'',
        beginTime:'',
        endTime:'',
        modifyTime:'',
        money:'',
        mid:'',
        paymentTime:'',
      }
    },
    //删除记录
    delMessage(data){
      this.$confirm('是否选择删除该条记录?一旦删除将无法恢复!', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delMemberMessage(data).then((res)=>{
              if(res.code == 200){
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getList()
              }
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //保留两位小数
    keepTwoDigits(data){
      var xsd=data.toString().split( "." );
      if (xsd.length==1){
        data =data .toString()+ ".00" ;
      return data ;
      }
      if (xsd.length>1){
      if (xsd[1].length<2){
        data =data .toString()+ "0" ;
      }
      return data ;
      }
    }
  },
};
</script>
<style lang="scss" scoped>
::v-deep{
  .el-dialog__body {
    padding: 30px 20px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
    padding-left: 40px;
  }
}
.input{
  width:270px
}
</style>
